<?php
function listing_location_admin_settings_form() {
  
  $result = db_query("SELECT * FROM {zipcodes} WHERE state='HBK'");
  
  while ($row = db_fetch_object($result)) {
    //drupal_set_message('state '.$row->state);
    db_query("UPDATE {zipcodes} SET state='HKB' WHERE city='%s'", $row->city);
	drupal_set_message('state '.$row->state);
	  //db_query("DELETE FROM {zipcodes} WHERE city='%s' AND state=00");
  }
  /*
  $result = db_query("SELECT * FROM {zipcodes_nz}");
  
  while ($row = db_fetch_object($result)) {
    drupal_set_message('state '.$row->city);
    $count = db_result(db_query("SELECT COUNT(*) FROM {zipcodes} WHERE city='%s'", $row->city));
	if (!$count) {
	  drupal_set_message(' adding '. $row->city);
	  db_query("INSERT INTO {zipcodes} ( zip, city, state, longitude, latitude, country) VALUES ( %d, '%s', '%s', %f, %f, '%s')", $row->zip, $row->city, $row->state, $row->longitude, $row->latitude, strtolower($row->country));
	}
  }
 */
 $form['listing_default_country'] = array(
    '#type'          => 'select',
	'#options'		 => location_get_iso3166_list(),
    '#title'         => t('Default Country'),
	'#description'   => t('This is the default country for listings.'),
    '#default_value' => variable_get('listing_default_country', 'nz'),
  );
  $form['listing_default_location'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Default Location'),
	'#description'   => t('In the absence of the geoip module, we need a default location to ensure the search has a starting point.  This should be in the form [city-name]: [state/province code] eg Chicago: IL.  If this is incorrect the site will fail for new users. Note that this functionality applies only to those countries with the necessary data available in the location module zipcodes table.  If you are using a country which does not have that data then use this module '),
    '#default_value' => variable_get('listing_default_location', 'Chicago: IL'),
  );
  $form['listing_location_include_zipcode'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Include Zipcode Search'),
	'#description'   => t('Some countries make greater use of zipcodes than others.  When we include zipcodes then the user has the option of entering a string or a zipcode number in the search field. Check the zipcode database first to ensure that your country has zipcodes.'),
    '#default_value' => variable_get('listing_location_include_zipcode', 0),
  );
  //  We check if this is a single country listing
  $result = db_query("SELECT DISTINCT(country) FROM {zipcodes}");
  $c = 0;
  while ($row = db_fetch_object($result)) {
    $c++;
  }
  variable_set('listing_location_countries', $c);
  // We also check if this is a single state province listing.
  $result = db_query("SELECT DISTINCT(state) FROM {zipcodes}");
  $c = 0;
  while ($row = db_fetch_object($result)) {
    $c++;
  }
  variable_set('listing_location_single_province', $c);
  
  // This is a simple check to see that our views are in place
  if (_listing_location_check_views()) variable_set('listing_location_views', 1);
  else variable_set('listing_location_views', 0);
  
  //listing_enable();
  if (variable_get('listing_location_countries', 0)) {
    $ctypes = variable_get('listing_content_types', array());
    if (!empty($ctypes)) {
      $form['listing_location_ctypes'] = array(
        '#type'          => 'select',
	    '#options'		 => variable_get('listing_content_types', array()),
        '#title'         => t('Content Types'),
        '#default_value' => variable_get('listing_location_ctypes', array()),
        '#description'   => t('Listing content types to use for location search.  Note that these content types have to be <a href="/admin/listing/general">selected</a> for listings.'),
	    '#multiple'		 => TRUE
      );
    }
    else {
      $form['listing_location_ctypes'] = array(
        '#type'          => 'markup',
	    '#value'         => l(t('No content types are enabled for the listing module'), 'admin/listing/general'),
	  );
    }
    $form['listing_search'] = array(
      '#type' => 'fieldset',
	  '#title' => t('Listing Search'),
	  '#collapsible' => TRUE,
	  '#collapsed' => FALSE,
    );
    $form['listing_search']['listing_location_select_search'] = array(
      '#type'          => 'checkbox',
      '#title'         => t('Use select search'),
	  '#description'   => t('The default search is a proximity search using the location database. Check this box to use a city select search.  This is only viable if you reduce the number of cities in your search scope to a number that can be handled by a select form.'),
      '#default_value' => variable_get('listing_location_select_search', 0),
    );
  /*
  $categories = array();
  $vocabs = taxonomy_get_vocabularies();
  foreach ($vocabs as $vocab) {
    $categories[$vocab->vid] = $vocab->name;
  }
  $form['listing_search']['listing_select_search_vocab'] = array(
    '#type'          => 'select',
	'#options' 		 => $categories,
    '#title'         => t('Search Taxonomy'),
	'#description'   => t('The taxonomy to use in conducting location searches.'),
    '#default_value' => variable_get('listing_select_search_vocab', 0),
  );
  */
    $form['listing_location_include_map'] = array(
      '#type'          => 'checkbox',
      '#title'         => t('Include Map in Listings'),
	  '#description'   => t('Listings whose content type does not include Locations will not display a map.'),
      '#default_value' => variable_get('listing_location_include_map', 1),
    );
    $form['map'] = array(
      '#type' => 'fieldset',
	  '#title' => t('Listing Map'),
	  '#collapsible' => TRUE,
	  '#collapsed' => FALSE
    );
    $form['map']['listing_location_map_width'] = array(
      '#type'          => 'textfield',
      '#title'         => t('Map width'),
	  '#description'   => t('The width of the map on the web page in pixels.'),
      '#default_value' => variable_get('listing_location_map_width', 300),
    );
    $form['map']['listing_location_map_height'] = array(
      '#type'          => 'textfield',
      '#title'         => t('Map height'),
	  '#description'   => t('The height of the map on the web page in pixels.'),
      '#default_value' => variable_get('listing_location_map_height', 200),
    );
    $form['map']['listing_location_map_zoom'] = array(
      '#type'          => 'textfield',
      '#title'         => t('Map Zoom'),
	  '#description'   => t('The zoom of the map on the web page in pixels.'),
      '#default_value' => variable_get('listing_location_map_zoom', 6),
    );
    $form['listing_location_target_list_limit'] = array(
      '#type'          => 'textfield',
      '#title'         => t('Number of items in targeted listing block'),
      '#default_value' => variable_get('listing_location_target_list_limit', 3),
    );
    $form['listing_location_target_default_distance'] = array(
      '#type'          => 'textfield',
      '#title'         => t('Default proximity'),
	  '#description'   => t('Enter a value for the default proximity in views proximity filters'),
      '#default_value' => variable_get('listing_location_target_default_distance', 25),
    );
    $form['views_check'] = array(
      '#type' => 'markup',
	  '#value' => variable_get('listing_location_views', 0) ? '<div>'.t('Location Search Views are enabled').'</div>' : '<div style="color: red">'.t('Cannot find Location Search Views.  Please check instructions').'</div>',
    );
    switch(variable_get('listing_location_single_province', 0)) {
      case 0:
	    $value = t('There is no province data in the zipcodes table.  Please see instructions for inserting country zipcode data for the location module');
	    break;
      case 1:
	    $value = t('There is only one state / province in the zipcodes table so you may remove the state / province parameter from the url.');
	    break;
      default:
	    $value = t('Multiple state provinces are available so country parameter will show');
	    break;
    }
    $form['province_check'] = array(
      '#type' => 'markup',
	  '#value' => '<div>'.$value.'</div>',
    );
    if (variable_get('listing_location_single_province', 0)) {
      $form['listing_location_show_province_url'] = array(
        '#type'          => 'checkbox',
        '#title'         => t('Province State in url.'),
        '#description'   => t('Untick this box if you do not want to show the province / state in the url. Default is on.'),
        '#default_value' => variable_get('listing_location_show_province_url', 1)
      );
  
    }
  }
  switch(variable_get('listing_location_countries', 0)) {
    case 0:
	  $value = t('There is no data in the zipcodes table.  Please see instructions for inserting country zipcode data for the location module');
	  break;
    case 1:
	  $value = t('There is only one country in the zipcodes table so you may remove the country parameter from the url.');
	  break;
    default:
	  $value = t('Multiple countries are available so country parameter will show');
	  break;
  }
  $form['country_check'] = array(
    '#type' => 'markup',
	'#value' => '<div">'.$value.'</div>',
  );
  if (variable_get('listing_location_countries', 0)) {
    $form['listing_location_show_country_url'] = array(
      '#type'          => 'checkbox',
      '#title'         => t('Country in url.'),
      '#description'   => t('Untick this box if you do not want to show the country in the url. Default is on.'),
      '#default_value' => variable_get('listing_location_show_country_url', 1)
    );
  }
  $form['#submit'][] = 'listing_location_admin_settings_submit';
  return system_settings_form($form);
}
function listing_location_admin_settings_submit($form, $form_state) {
  cache_clear_all('*', 'cache');
}
function listing_location_admin_filter_explanation() {
  $output = '<p>Filterable content is determined by the contents of the zipcodes table which is created by the location module.  All records in this table are inserted by using the .sql file available in the location module directory database.  Currently only about a half a dozen countries are supported.  You may reduce the available records by filtering by province / state and by city.  It is advisable to filter by province first, otherwise the city select form will be unwieldly because it will include thousands of options.</p';
  return $output;
}
function listing_location_admin_provinces_form() {
  //listing_enable();
  $provinces = array();
  $result = db_query("SELECT DISTINCT(state) FROM zipcodes ORDER BY state");
  while ($row = db_fetch_object($result)) {
    $provinces[$row->state] = $row->state;
  }
  $form['explanation'] = array(
    '#type' 		 => 'markup',
	'#value'		 => t('Use this form if you want to restrict the states / provinces that are available on the search form autocomplete.')
  );
  $form['provinces'] = array(
    '#type'          => 'select',
	'#options'		 => $provinces,
    '#title'         => t('State / Provinces'),
	'#description'   => t('Select the states / provinces you wish to keep'),
    '#multiple'		 => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
	'#value' => t('Submit')
  );
  return $form;
}
function listing_location_admin_provinces_form_submit($form, $form_state) {
  db_query("DELETE FROM {zipcodes} WHERE state NOT IN ('". implode("','", $form_state['values']['provinces'])."')");
}
function listing_location_admin_cities_form() {
  //listing_enable();
  $cities = array();
  $result = db_query("SELECT DISTINCT(city) FROM zipcodes ORDER BY city");
  while ($row = db_fetch_object($result)) {
    $cities[$row->city] = $row->city;
  }
  $form['explanation'] = array(
    '#type' 		 => 'markup',
	'#value'		 => t('Use this form if you want to restrict the cities that are available on the search form autocomplete.  It is advisable to filter by province / state first.')
  );
  $form['cities'] = array(
    '#type'          => 'select',
	'#options'		 => $cities,
    '#title'         => t('Cities'),
	'#description'   => t('Select the cities you wish to keep'),
    '#multiple'		 => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
	'#value' => t('Submit')
  );
  return $form;
}
function listing_location_admin_cities_form_submit($form, $form_state) {
  db_query("DELETE FROM {zipcodes} WHERE city NOT IN ('". implode("','", $form_state['values']['cities'])."')");
}